function new_in() {
    let td_text_elements = document.querySelectorAll('tr td:nth-child(10)')
    for (let i = 0; i < td_text_elements.length; i++) {
        if (td_text_elements[i].innerText === '新入') {
            $(td_text_elements[i]).addClass('back2')
        }
    }
}


// 数据变表格
function table(datas) {
    let table1 = '<table><tr>'
    for (let i = 0; i < datas[0].length; i++) {
        table1 += `<th>${datas[0][i]}</th>`
    }
    table1 += '</tr>'
    for (let i = 0; i < datas[1].length; i++) {
        table1 += '<tr>'
        for (let j = 0; j < datas[1][i].length; j++) {
            if (datas[1][i][j] === 'None' || datas[1][i][j] == 0) {
                table1 += `<td></td>`
            } else {
                table1 += `<td>${datas[1][i][j]}</td>`
            }
        }
        table1 += '</tr>'
    }
    table1 += '</table>'
    return table1
}

// 带选择框的表格
function checkbox_table(datas, index = 0) {
    let rows = []
    rows.push('<table>')
    let row_head = ['<tr><th>选择</th>']
    for (let i = 0; i < datas[0].length; i++) {
        row_head.push(`<th>${datas[0][i]}</th>`)
    }
    row_head.push('</tr>')
    rows.push(row_head.join(''))
    for (let i = 0; i < datas[1].length; i++) {
        let row_body = [`<tr><td><input type="checkbox" name="${datas[1][i][index]}"></td>`]
        for (let j = 0; j < datas[1][i].length; j++) {
            if (datas[1][i][j] === 'None' || datas[1][i][j] == 0) { row_body.push(`<td></td>`) } else { row_body.push(`<td>${datas[1][i][j]}</td>`) }

        }
        row_body.push('</tr>')
        rows.push(row_body.join(''))
    }
    rows.push('</table>')
    return rows.join('')

}

function form_table(datas, index = 0) {
    let rows = []
    rows.push(`<form action='' method='get' >`)
    rows.push(checkbox_table(datas, index = index))
    rows.push(`</form>`)
    return rows.join('')
}

//  带圆的字
function circle_font(el, str1) {
    for (let i = 0; i < str1.length; i++) {
        el.append(`<div>${str1[i]}</div>`)
    }
}

// 渲染版心的内容 datas 相当于mysql中的sql语名
function test1(datas) {
    $.post('/sql/', { sql: datas }, function (data, status) {
        $('div.wrapper').empty().append(table(data))
        new_in()
    })
}

function getData(sql, elementClass) {
    $.post('/sql/', { sql: sql }, function (data, status) {
        $(elementClass).empty().append(table(data))
    })
}

function getDomData(sql, elementClass, fun = table) {
    $.post('/sql/', { sql: sql }, function (data, status) {
        $(elementClass).empty().append(fun(data))
        new_in()
    })
}

// 渲染版心 带选择的内容
function show_checkbox_table(sql, index = 2) {
    $.post('/sql/', { sql: sql }, function (data, status) {
        $('div.wrapper').empty().append(checkbox_table(data, index))
    })
}


// 渲染版心内容
function wrapper_content() {
    $.get('/sql/', function (data, status) {
        $('div.wrapper').empty().append(table(data))
        new_in()
    })
}

// 渲染版心题头
function wrapper_title_1() {
    $.post('/sql/', { sql: 'select dept,count(*) from patients a left join employee b on a.管床医生=b.name where 出院日期 is null group by dept order by dept desc' }, function (data, status) {
        let title2 = data[1]
        let title3 = ''
        let totlo = 0
        for (let i = 0; i < title2.length; i++) {
            if (i === 0) { title3 += `<span class='back1' name='${title2[i][0]}'>${title2[i][0]}${title2[i][1]}人</span>` } else { title3 += `<span name='${title2[i][0]}'>${title2[i][0]}${title2[i][1]}人</span>` }
            totlo += +title2[i][1]
        }
        title3 += `<span name='全院'>全院${totlo}人</span>`
        $('#two').empty().append(title3)
        // console.log(status)
    })
}

// 渲染版心题头2
function wrapper_title() {
    $.post('/sql/', { sql: 'select dept,count(*),sum(case when 入院日期=date(now()) then 1 else 0 end) from patients a left join employee b on a.管床医生=b.name where 出院日期 is null group by dept order by dept desc' }, function (data, status) {
        let title2 = data[1]
        let title3 = ''
        let totlo = 0
        let totlo_2 = 0
        for (let i = 0; i < title2.length; i++) {
            if (i === 0) { title3 += `<span class='back1' name='${title2[i][0]}'>${title2[i][0]}${title2[i][1]}人<i style="font-size:8px;margin-left:10px">入${title2[i][2]}</i></span>` } else { title3 += `<span name='${title2[i][0]}'>${title2[i][0]}${title2[i][1]}人<i style="font-size:8px ;margin-left:10px">入${title2[i][2]}</i></span>` }
            totlo += +title2[i][1]
            totlo_2 += +title2[i][2]
        }
        title3 += `<span name='全院'>全院${totlo}人<i style="font-size:8px;margin-left:10px">入${totlo_2}</i></span>`
        $('#two').empty().append(title3)
        // console.log(status)
    })
}


// 手术统计
function statistics_operation() {
    let sql = `select year(手术日期) 月 from operation group by 月`
    $.post('/sql', { sql: sql }, function (data) {
        let datas = data[1]
        let temp = ''
        for (let i = 0; i < datas.length; i++) {
            temp += `sum(case when year(手术日期)=${datas[i]} then 1 else 0 end)  as ${datas[i]}年,`
        }
        let sql2 = `
        SELECT
        MONTH(手术日期) 月,
        ${temp}
        count(*)  合计
        from operation
        GROUP BY 月
        UNION
        select 
        '合计' ,
        ${temp}
        count(*)
        FROM operation
        `
        $('#two').empty().append(`<span class='back1'>手术统计</span>`)
        test1(sql2)
    })

}

// 费用统计
function statistics_expense() {
    let sql = `select year(出院日期) 年 from patients where 出院日期 is not null group by 年`
    $.post('/sql', { sql: sql }, function (data) {
        let datas = data[1]
        let temp = ''
        for (let i = 0; i < datas.length; i++) {
            temp += `sum(case when year(出院日期)=${datas[i]} then 费用 else 0 end)  as ${datas[i]}年,`
        }
        let sql2 = `
        SELECT
        MONTH(出院日期) 月,
        ${temp}
        sum(费用)  合计
        from patients
        where 出院日期 is not null
        GROUP BY 月
        UNION
        select 
        '合计' ,
        ${temp}
        sum(费用)
        FROM patients
        where 出院日期 is not null
        `
        $('#two').empty().append(`<span class='back1'>费用统计</span>`)
        test1(sql2)
    })

}

// 自费统计
function statistics_own_expense() {
    let sql = `select year(出院日期) 年 from patients where 出院日期 is not null group by 年`
    $.post('/sql', { sql: sql }, function (data) {
        let datas = data[1]
        let temp = ''
        for (let i = 0; i < datas.length; i++) {
            temp += `sum(case when year(出院日期)=${datas[i]} then 自费 else 0 end)  as ${datas[i]}年,`
        }
        let sql2 = `
        SELECT
        MONTH(出院日期) 月,
        ${temp}
        sum(自费)  合计
        from patients a
        left join own_expense b on a.住院号=b.住院号
        where 出院日期 is not null
        GROUP BY 月
        UNION
        select 
        '合计' ,
        ${temp}
        sum(自费)
        FROM patients a
        left join own_expense b on a.住院号=b.住院号
        where 出院日期 is not null    
        `
        $('#two').empty().append(`<span class='back1'>自费统计</span>`)
        test1(sql2)
    })

}

function test2() {
    let sql = `select * from employee`
    $('#two').empty().append(`<span class='back1'>自费统计</span>`)
    show_checkbox_table(sql)
}

// 添加手术
function operation_date() {
    let data = $('.formdata').serialize()
    let sql = `replace into operation select 住院号,date(now()) from patients where 住院号 in ()`
}

// 医学公式

function blood_glucose() {
    let bg = document.querySelector('[name=bg]')
    let bw = document.querySelector('[name=bw]')
    let result = document.querySelector('[name=result]')
    result.value = (+bg.value - 5.6) * 0.6 * (+bw.value) / 11.1
    result.value = Math.floor(result.value * 100) / 100
}

function medical() {
    $('#two').empty().append(`<span class='back1'>血糖公式</span>`)
    $('div.wrapper').empty().append(`<div>
    <label for="bg">血糖（mmol/L）</label>
    <input id="bg" type="text" name="bg" value="18"><br>
    <label for="bw">体重（kg）</label>
    <input id="bw" type="text" name="bw" value="50"><br>
    <label for="result">胰岛素用量（UI）</label>
    <input type="text" name="result" readonly>    
</div>
`)
    blood_glucose()
}

function getTodayDate() {
    const today = new Date(); // 获取当前日期和时间
    const year = today.getFullYear(); // 获取年份
    const simpleYear = String(year).slice(-2)  //简单年份
    const month = String(today.getMonth() + 1).padStart(2, '0'); // 获取月份，+1 是因为月份从 0 开始，padStart 用于补零
    const day = String(today.getDate()).padStart(2, '0'); // 获取日期，padStart 用于补零

    return `${simpleYear}${month}${day}`; // 返回格式化后的日期字符串
}

console.log(getTodayDate()); // 输出格式为 YYYY-MM-DD